package cn.edu.lingnan.service.imp;



import cn.edu.lingnan.dao.ScoreMapper;
import cn.edu.lingnan.dao.StudentMapper;
import cn.edu.lingnan.pojo.Student;
import cn.edu.lingnan.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Scanner;

@Service
public class StudentServiceImp implements StudentService {
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    ScoreMapper scoreMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insert() {
        Student stu1=new Student();
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入学号");
        stu1.setSid(sc.next());
        System.out.println("请输入名字");
        stu1.setSname(sc.next());
        System.out.println("请输入密码");
        stu1.setPassword(sc.next());
        System.out.println("请设置不知道什么标志");
        stu1.setSright(sc.nextInt());
        System.out.println("请再设置不知道什么标志");
        stu1.setSflag(sc.nextInt());
        return studentMapper.insert(stu1);
    }

    @Override
    public Student getStudentByID(String sid) {
        return studentMapper.getStudentByID(sid);
    }

    @Override
    public List<Student> getStudentLikeName(String sname) {
        return studentMapper.getStudentLikeName(sname);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateById() {
        Student stu1=new Student();
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入学号");
        stu1.setSid(sc.next());
        System.out.println("请输入修改后名字");
        stu1.setSname(sc.next());
        System.out.println("请输入修改后密码");
        stu1.setPassword(sc.next());
        System.out.println("请设置不知道什么修改后标志");
        stu1.setSright(sc.nextInt());
        System.out.println("请再设置不知道什么修改后标志");
        stu1.setSflag(sc.nextInt());
        return studentMapper.updateById(stu1);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public  Integer deleteById(String sid) {
         Integer count=scoreMapper.deleteBySid(sid); //先删除有外键约束的信息
        System.out.println("成功删除"+count+"条分数数据");
        return studentMapper.deleteById(sid);
    }


    public void StudentActions() {
        System.out.println("                --欢迎进入学生信息管理系统--");
        System.out.println("----------------------------------------------------------------");
        while (true){
            System.out.println(" 1:添加学生信息  2:删除学生信息 3:修改学生信息 4:查询学生信息 0:退出");
            Scanner sc=new Scanner(System.in);
            Integer  selectflag=sc.nextInt();
            if (selectflag ==0){
                System.out.println("已退出学生信息管理系统");
                break;
            }
            switch (selectflag){
                case 1:Integer n1= insert();
                    System.out.println("成功插入"+n1+"条学生数据");
                    break;

                case 2:
                    System.out.println("请输入你需要删除学生的id");
                    Integer n2=deleteById(sc.next());
                    System.out.println("成功删除"+n2+"条学生数据");
                    break;

                case 3:
                    Integer n= updateById();
                    System.out.println("成功修改"+n+"条学生数据");
                    break;

                case 4:
                    while (true){
                        System.out.println("1:根据id查询学生信息 2:根据姓氏模糊查询 3:退出");
                        Integer flag=sc.nextInt();
                        if(flag==1){
                            System.out.println("请输入id");
                            Student student=  getStudentByID(sc.next());
                            System.out.println(student);
                        }
                        else if (flag==2){
                            System.out.println("请输入姓氏");
                            List<Student> list=getStudentLikeName(sc.next());
                            System.out.println(list);
                        }
                        else break;
                    }
                    break;
                default:
                    System.out.println("您的输入有误");
                    break;
            }

        }
    }
}
